﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;


namespace HRM
{
    public partial class frmChangeBranch : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        string _OrderBy = "";
        string _OrderDirection = "";
        int BranchID;
        string _BranchCode = "";
        BranchClient wcfBranch;
        RoleRightInfo listRole;
        public frmChangeBranch()
        {
            InitializeComponent();
            //LoadLanguge();
            wcfBranch = Service.Branch();

            listRole = UIParamater.Quyen_ChiNhanh;
            
            
            BuildVoucherGrid();
            LoadData();
            this.KeyPreview = true;

        }
        void LoadLanguge()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_ContextMenuStrip(ref mnuPopUp);
        }

        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "Branch_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_Code", "", "", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Branch_Name", "", "", 200, true, true);

            UISetting.AddColumn(ref dgvList, "Branch_BHXH", "", "", 100, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_Address", "", "", 250, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_Phone", "", "", 100, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_Fax", "", "", 100, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_RepName", "", "", 140, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_RepPos", "", "", 100, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_Description", "", "", 200, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_MedicalInsurancePercent", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_SocialInsurancePercent", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_MedicalInsurancePercentLabour", "", "", 200, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_SocialInsurancePercentLabour", "", "", 200, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_MinBasicSalary", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_ManagementLevel", "", "", 200, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_FamilySalary", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_CloseBook", "", "", 150, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_Joining", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_TradeUnion", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_UnemployedInsCompany", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_UnemployedIns", "", "", 120, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_TotalDateWork", "", "", 150, false, true);

        }
        private void LoadData()
        {
            if (Program.isAdmin)
                _BranchCode = "";
            else
                _BranchCode = Program.strBranchCode;
            DataSet _ds = wcfBranch.Branch_GetList(out itemCount, currPage, pageSize, _BranchCode);
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["Branch_ID"].Value = _ds.Tables[0].Rows[i]["Branch_ID"].ToString();
                dgvList.Rows[i].Cells["Branch_Code"].Value = _ds.Tables[0].Rows[i]["Branch_Code"].ToString();
                dgvList.Rows[i].Cells["Branch_BHXH"].Value = _ds.Tables[0].Rows[i]["Branch_BHXH"].ToString();
                dgvList.Rows[i].Cells["Branch_Name"].Value = _ds.Tables[0].Rows[i]["Branch_Name"].ToString();
                dgvList.Rows[i].Cells["Branch_Address"].Value = _ds.Tables[0].Rows[i]["Branch_Address"].ToString();
                dgvList.Rows[i].Cells["Branch_Phone"].Value = _ds.Tables[0].Rows[i]["Branch_Phone"].ToString();
                dgvList.Rows[i].Cells["Branch_Fax"].Value = _ds.Tables[0].Rows[i]["Branch_Fax"].ToString();
                dgvList.Rows[i].Cells["Branch_RepName"].Value = _ds.Tables[0].Rows[i]["Branch_RepName"].ToString();
                dgvList.Rows[i].Cells["Branch_RepPos"].Value = _ds.Tables[0].Rows[i]["Branch_RepPos"].ToString();
                dgvList.Rows[i].Cells["Branch_MedicalInsurancePercent"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_MedicalInsurancePercent"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["Branch_SocialInsurancePercent"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_MedicalInsurancePercent"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["Branch_MedicalInsurancePercentLabour"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_MedicalInsurancePercent"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["Branch_SocialInsurancePercentLabour"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_MedicalInsurancePercent"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["Branch_MinBasicSalary"].Value = _ds.Tables[0].Rows[i]["Branch_MinBasicSalary"].ToString();
                dgvList.Rows[i].Cells["Branch_ManagementLevel"].Value = _ds.Tables[0].Rows[i]["Branch_ManagementLevel"].ToString();
                dgvList.Rows[i].Cells["Branch_FamilySalary"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_FamilySalary"]).ToString(UIParamater.Phay2So);
                dgvList.Rows[i].Cells["Branch_CloseBook"].Value = _ds.Tables[0].Rows[i]["Branch_CloseBook"].ToString();
                dgvList.Rows[i].Cells["Branch_Joining"].Value = _ds.Tables[0].Rows[i]["Branch_Joining"].ToString();
                dgvList.Rows[i].Cells["Branch_TradeUnion"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_TradeUnion"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["Branch_UnemployedInsCompany"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_UnemployedInsCompany"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["Branch_UnemployedIns"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Branch_UnemployedIns"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["Branch_TotalDateWork"].Value = _ds.Tables[0].Rows[i]["Branch_TotalDateWork"].ToString();
                dgvList.Rows[i].Cells["Branch_Description"].Value = _ds.Tables[0].Rows[i]["Branch_Description"].ToString();
            }
        }

        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                UISetting.MesNotRightNew();
                return;
            }
            Form f = new frmBranchInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    BranchID = Convert.ToInt32(dgvList.CurrentRow.Cells["Branch_ID"].Value);
                    Form f = new frmBranchInfo(BranchID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    UISetting.MesNotRightView();

            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmBranchList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Close();
                    this.Dispose();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
            }
        }
        private void dgvList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvList.SelectedRows.Count > 0)
                {
                    mnuPopUp.Enabled = true;
                    btnNewBranch.Enabled = true;
                    btnEditBranch.Enabled = true;
                }
            }
        }

        private void btnNewBranch_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditBranch_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void frmBranchList_Activated(object sender, EventArgs e)
        {
            //   LoadLanguge();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["Branch_Code"].HeaderText = "Mã";
                dgvList.Columns["Branch_BHXH"].HeaderText = "Mã BHXH";
                dgvList.Columns["Branch_Name"].HeaderText = "Tên";
                dgvList.Columns["Branch_Address"].HeaderText = "Địa chỉ";
                dgvList.Columns["Branch_Phone"].HeaderText = "Điện thoại";
                dgvList.Columns["Branch_Fax"].HeaderText = "Fax";
                dgvList.Columns["Branch_RepName"].HeaderText = "Người phụ trách";
                dgvList.Columns["Branch_RepPos"].HeaderText = "Chức vụ";
                dgvList.Columns["Branch_MedicalInsurancePercent"].HeaderText = "BHYT Cty";
                dgvList.Columns["Branch_SocialInsurancePercent"].HeaderText = "BHXH Cty";
                dgvList.Columns["Branch_MedicalInsurancePercentLabour"].HeaderText = "BHYT NLĐ";
                dgvList.Columns["Branch_SocialInsurancePercentLabour"].HeaderText = "BHXH NLĐ";
                dgvList.Columns["Branch_MinBasicSalary"].HeaderText = "Lương tối thiểu";
                dgvList.Columns["Branch_ManagementLevel"].HeaderText = "Cấp quản lý";
                dgvList.Columns["Branch_FamilySalary"].HeaderText = "Giảm trừ gia cảnh";
                dgvList.Columns["Branch_CloseBook"].HeaderText = "Ngày chốt sổ lương";
                dgvList.Columns["Branch_Joining"].HeaderText = "Ngày tính lương";
                dgvList.Columns["Branch_TradeUnion"].HeaderText = "Phí công đoàn ";
                dgvList.Columns["Branch_UnemployedInsCompany"].HeaderText = "BHTN Cty";
                dgvList.Columns["Branch_UnemployedIns"].HeaderText = "BHTN NLĐ";
                dgvList.Columns["Branch_TotalDateWork"].HeaderText = "Số ngày làm việc ";
                dgvList.Columns["Branch_Description"].HeaderText = "Ghi chú";
            }
        }

        private void frmBranchList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfBranch.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                BranchID = Convert.ToInt32(dgvList.CurrentRow.Cells["Branch_ID"].Value);
                //if (wcfBranch.Branch_Using(BranchID) != 0)
                //{
                //    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    return;
                //}
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfBranch.Branch_Delete(BranchID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }
        }

        private void dgvList_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //MessageBox.Show(dgvList.SortOrder.ToString());           
            //MessageBox.Show(dgvList.SortedColumn.Name);

            _OrderBy = dgvList.SortedColumn.Name;
            switch (dgvList.SortOrder.ToString())
            {
                case "Ascending":
                    _OrderDirection = "ASC";
                    break;
                case "Descending":
                    _OrderDirection = "DESC";
                    break;
            }
            LoadData();
        }
    }
}
